package schoolWork.work05.test8;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyJFrame extends JFrame implements ActionListener {
    JFrame jFrame = new JFrame("请选择你喜欢的食物");
    Container cont = jFrame.getContentPane();
    // 创建一个水平方向的Box
    Box box1 = Box.createHorizontalBox();

    JLabel nameLabel = new JLabel("姓名");
    JTextField nameText = new JTextField(10);

    JLabel sexLabel = new JLabel("性别");
    JRadioButton manRadio = new JRadioButton("男");
    JRadioButton womanRadio = new JRadioButton("女");
    ButtonGroup group = new ButtonGroup();

    JComboBox cmb = new JComboBox(new String[]{
            "计算机系", "自动化系", "数学系", "物理系"
    });
    //创建一个网格式布局的pan1
    JPanel pan1 = new JPanel(new GridLayout(1, 4));
    JCheckBox mainfood1 = new JCheckBox("米饭");
    JCheckBox mainfood2 = new JCheckBox("面条");
    JCheckBox mainfood3 = new JCheckBox("粉丝");
    JCheckBox mainfood4 = new JCheckBox("水饺");

    //创建一个网格式布局的pan2
    JPanel pan2 = new JPanel(new GridLayout(1, 4));
    JCheckBox secondfood1 = new JCheckBox("清炒时蔬");
    JCheckBox secondfood2 = new JCheckBox("红烧小排");
    JCheckBox secondfood3 = new JCheckBox("清蒸河鱼");
    JCheckBox secondfood4 = new JCheckBox("红烧鸭块");
    //创建一个网格式布局的pan3
    JPanel pan3 = new JPanel(new GridLayout(1, 4));
    JCheckBox soupfood1 = new JCheckBox("番茄菜汤");
    JCheckBox soupfood2 = new JCheckBox("菌菇汤");
    JCheckBox soupfood3 = new JCheckBox("酸菜粉丝汤");
    JCheckBox soupfood4 = new JCheckBox("青菜汤");
    //创建一个网格式布局的pan4
    JPanel pan4 = new JPanel(new GridLayout(1, 2));
    JButton btn1 = new JButton("确定");
    JButton btn2 = new JButton("取消");


    public MyJFrame() {
        cont.setLayout(new GridLayout(5, 1));

        //对box1的操作
        group.add(manRadio);
        group.add(womanRadio);
        manRadio.setSelected(true);

        cmb.setSelectedIndex(0);
        cmb.setEditable(true);

        box1.add(nameLabel);
        box1.add(Box.createHorizontalStrut(8));
        box1.add(nameText);
        box1.add(Box.createHorizontalStrut(8));
        box1.add(sexLabel);
        box1.add(Box.createHorizontalStrut(8));
        box1.add(manRadio);
        box1.add(womanRadio);
        box1.add(cmb);

        cont.add(box1);

        //对面板pan1的操作
        pan1.setBorder(BorderFactory.createTitledBorder("主食"));
        pan1.add(mainfood1);
        pan1.add(mainfood2);
        pan1.add(mainfood3);
        pan1.add(mainfood4);
        cont.add(pan1);

        //对面板pan2的操作
        pan2.setBorder(BorderFactory.createTitledBorder("菜肴"));
        pan2.add(secondfood1);
        pan2.add(secondfood2);
        pan2.add(secondfood3);
        pan2.add(secondfood4);
        cont.add(pan2);

        //对面板pan3的操作
        pan3.setBorder(BorderFactory.createTitledBorder("汤羹"));
        pan3.add(soupfood1);
        pan3.add(soupfood2);
        pan3.add(soupfood3);
        pan3.add(soupfood4);
        cont.add(pan3);

        //对面板pan4的操作
        pan4.add(btn1);
        pan4.add(btn2);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        cont.add(pan4);

        this.jFrame.setSize(500, 400);
        this.jFrame.setLocationRelativeTo(null);
        this.jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.jFrame.setVisible(true);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn1) {
            String name = nameText.getText();
            String sex;
            if (manRadio.isSelected())
                sex = "男";
            else
                sex = "女";
            String department = cmb.getSelectedItem().toString();

            StringBuffer food = new StringBuffer();
            if (mainfood1.isSelected())
                food.append(mainfood1.getText() + " ");
            if (mainfood2.isSelected())
                food.append(mainfood2.getText() + " ");
            if (mainfood3.isSelected())
                food.append(mainfood3.getText() + " ");
            if (mainfood4.isSelected())
                food.append(mainfood4.getText() + " ");
            if (secondfood1.isSelected())
                food.append(secondfood1.getText() + " ");
            if (secondfood2.isSelected())
                food.append(secondfood2.getText() + " ");
            if (secondfood3.isSelected())
                food.append(secondfood3.getText() + " ");
            if (secondfood4.isSelected())
                food.append(secondfood4.getText() + " ");
            if (soupfood1.isSelected())
                food.append(soupfood1.getText() + " ");
            if (soupfood2.isSelected())
                food.append(soupfood2.getText() + " ");
            if (soupfood3.isSelected())
                food.append(soupfood3.getText() + " ");
            if (soupfood4.isSelected())
                food.append(soupfood4.getText() + " ");
            String Food = food.toString();

            //打印记录的内容
            System.out.println(name + " " + sex + " " + department + " " + "喜欢吃的食物有:");
            System.out.println(Food);
        } else if (e.getSource() == btn2) {
            //清除选项
            nameText.setText(null);
            manRadio.setSelected(false);
            womanRadio.setSelected(false);
            cmb.setSelectedIndex(0);
            mainfood1.setSelected(false);
            mainfood2.setSelected(false);
            mainfood3.setSelected(false);
            mainfood4.setSelected(false);
            secondfood1.setSelected(false);
            secondfood2.setSelected(false);
            secondfood3.setSelected(false);
            secondfood4.setSelected(false);
            soupfood1.setSelected(false);
            soupfood2.setSelected(false);
            soupfood3.setSelected(false);
            soupfood4.setSelected(false);
        }
    }
}
